<?php

namespace App\Console\Commands;

use App\Models\City;
use App\Models\City1;
use Illuminate\Console\Command;

class CityCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'city';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $citys = City::where('parent_id', 0)->get();

        foreach ($citys as $city2) {
          $first=  City1::where('cityname','like', $city2->name."%")->first();
            $city1 = City1::where('pid', $first->id)->get();

            foreach ($city1 as $item) {
                if (City::where('name', $item->cityname)->first()) {
                    continue;
                };
                $city = new City();
                $city->name = $item->cityname;
                $city->parent_id = $city2->id;
                $city->pinyin = $item->pinyin;
                $city->first = $item->first;
                $city->lat = $item->latitude;
                $city->lng = $item->longitude;
                $city->save();

            }
        }

    }
}
